package com.tmall.testgen.common.util;

import java.net.URLClassLoader;
import java.util.List;

import com.tmall.testgen.client.config.ConfigUtil;
import com.tmall.testgen.client.config.TargetProjectConfig;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo4Transfer;
//import org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.XStream;

/**
 * @author haojia
 * @date 2014-07-04
 */
public class InterceptorConvertor {

	private static URLClassLoader classLoader;
	private static URLClassLoader getClassLoader(String targetProjectName) throws Exception{
		if(classLoader!=null)
			return classLoader;
		
		classLoader=ConfigUtil.getClassLoader(targetProjectName);
		return classLoader;
	}
	public static InterceptInfo4Transfer interceptInfo2TransferInterceptInfo( InterceptInfo info){
		XStream xstream = new XStream(); 
		
		
		InterceptInfo4Transfer n=new InterceptInfo4Transfer();
		n.setClassName(info.getClassName());
		n.setClassSimpleName(info.getClassSimpleName());
		n.setException(info.getException());
		n.setMethodName(info.getMethodName());
		n.setParamNames(info.getParamNames());
		n.setMethodParamContent(info.getMethodParamContent());
		n.setBeanName(info.getBeanName());
		n.setExecuteTime(info.getExecuteTime());
		n.setType(info.getType());
		if(info.getParams()!=null)
			n.setParamXML(xstream.toXML(info.getParams()));
		if(info.getResult()!=null)
			n.setResultXML(xstream.toXML(info.getResult()));
		return n;
	}
	
	public static InterceptInfo transferInterceptInfo2InterceptInfo(InterceptInfo4Transfer info) throws Exception{
		
		URLClassLoader classLoader=	getClassLoader(TargetProjectConfig.getInstance().getTargetProjectName());
		XStream xstream = new XStream(null,new XppDriver(),classLoader,null); 
		
		
		InterceptInfo n=new InterceptInfo();
		n.setClassName(info.getClassName());
		n.setClassSimpleName(info.getClassSimpleName());
		n.setException(info.getException());
		n.setMethodName(info.getMethodName());
		n.setParamNames(info.getParamNames());
		n.setMethodParamContent(info.getMethodParamContent());
		n.setBeanName(info.getBeanName());
		n.setExecuteTime(info.getExecuteTime());
		n.setType(info.getType());
		if(info.getParamXML()!=null)
			n.setParams((List<Object>) xstream.fromXML(info.getParamXML()));
		if(info.getResultXML()!=null)
			n.setResult(xstream.fromXML(info.getResultXML()));
		return n;
	}
//	public static String getXml(Object root) throws Exception{
//		ClassLoader c;
//		c.
//		StringWriter sw = new StringWriter();  
//        JAXBContext jaxbContext = JAXBContext.newInstance(root.getClass());  
//        Marshaller marshaller = jaxbContext.createMarshaller();  
//        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);  
//        marshaller.marshal(root, sw); 
//        return sw.toString();
//	}

}
